﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Linq;
using Newtonsoft.Json;
using System.IO;

namespace Mail.MiniProgram.Core.Helpers
{
    public class HttpHelper
    {
        /// <summary>
        /// Get方法请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <returns></returns>
        public  static T Get<T>(string url, Dictionary<string, string> headers = null)
        {
            using (var httpClient = new HttpClient())
            {
                HttpRequestMessage request = new HttpRequestMessage()
                {
                    RequestUri = new Uri(url),
                    Method = HttpMethod.Get,
                };
                if (headers != null && headers.Count > 0)
                {
                    request.Headers.Clear();

                    foreach (var header in headers)
                    {
                        request.Headers.Add(header.Key, header.Value);

                    }
                }
                HttpResponseMessage response = httpClient.SendAsync(request).Result;

                var responseString = response.Content.ReadAsStringAsync().Result;
                return JsonConvert.DeserializeObject<T>(responseString);
            }

        }


        /// <summary>
        /// Get stream
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <returns></returns>
        public static Stream GetStream(string url, Dictionary<string, string> headers = null)
        {
            using (var httpClient = new HttpClient())
            {
                HttpRequestMessage request = new HttpRequestMessage()
                {
                    RequestUri = new Uri(url),
                    Method = HttpMethod.Get,
                };
                if (headers != null && headers.Count > 0)
                {
                    request.Headers.Clear();

                    foreach (var header in headers)
                    {
                        request.Headers.Add(header.Key, header.Value);

                    }
                }
                HttpResponseMessage response = httpClient.SendAsync(request).Result;

                return response.Content.ReadAsStreamAsync().Result;

            }

        }



        /// <summary>
        /// Post方法请求 application/x-www-form-urlencoded格式
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="paramList">参数集合</param>
        /// <returns></returns>
        public static T Post<T>(string url, List<KeyValuePair<String, String>> paramList, Dictionary<string, string> headers = null)
        {
            using (var httpClient = new HttpClient())
            {
                FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent(paramList);
                if (headers != null && headers.Count > 0)
                {
                    formUrlEncodedContent.Headers.Clear();
                    foreach (var header in headers)
                    {
                        formUrlEncodedContent.Headers.Add(header.Key, header.Value);
                    }
                }
                HttpResponseMessage response = httpClient.PostAsync(new Uri(url), formUrlEncodedContent).Result;

                var resString = response.Content.ReadAsStringAsync().Result;

                return JsonConvert.DeserializeObject<T>(resString);
            }

        }


        /// Post方法请求 application/json格式
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="paramList">参数集合</param>
        /// <returns></returns>
        public static T Post<T>(string url, object paramObj, Dictionary<string, string> headers = null)
        {
            using (var httpClient = new HttpClient())
            {
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(paramObj);

                StringContent stringContent = new StringContent(json);
                if (headers != null && headers.Count > 0)
                {
                    stringContent.Headers.Clear();
                    foreach (var header in headers)
                    {
                        stringContent.Headers.Add(header.Key, header.Value);
                    }
                }
                HttpResponseMessage response = httpClient.PostAsync(new Uri(url), stringContent).Result;

                var resString = response.Content.ReadAsStringAsync().Result;

                return JsonConvert.DeserializeObject<T>(resString);
            }

        }




    }
}
